/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bakenote.data;

import bakenote.core.Recipe;
import bakenote.core.RecipeGroup;
import org.apache.pivot.collections.*;

import java.util.Iterator;

/**
 * @author lello
 */
public class RecipeList {

    // This array contains the list of all the formulae actually loaded
    // by the program. It is accessible in the entire program.
    private final static List<Recipe> recipeList = new ArrayList<Recipe>();
    private final static Map<String, Recipe> recipeMap = new HashMap<String, Recipe>();

    //public RecipeList () { }

    public static void init() {
        // clear the list (just to be safe)
        recipeList.clear();

        recipeMap.getMapListeners().add(new MapListener.Adapter<String, Recipe>() {

            @Override
            public void valueAdded(Map<String, Recipe> map, String k) {
                //System.out.println("adding recipe in list" +map.get(k).getName());
                recipeList.add(map.get(k));
            }

            @Override
            public void valueUpdated(Map<String, Recipe> map, String k, Recipe previousValue) {
                //System.out.println("updating recipe in list " +map.get(k).getName());
                int i = recipeList.indexOf(previousValue);
                recipeList.update(i, map.get(k));
            }

            @Override
            public void valueRemoved(Map<String, Recipe> map, String k, Recipe v) {
                //System.out.println("removing recipe in list" +map.get(k).getName());
                recipeList.remove(v);
            }

        });
    }

    public static void filter(String s, RecipeGroup rg) {
        recipeList.clear();

        if (rg != null) {
            if (s != null) {
                for (Recipe r : rg.getList()) {
                    if (r.contains(s)) {
                        recipeList.add(r);
                    }
                }
            } else {
                for (Recipe r : rg.getList()) {
                    recipeList.add(r);
                }
            }

        } else {
            Iterator it = recipeMap.iterator();
            if (s != null) {
                while (it.hasNext()) {
                    String key = (String) it.next();
                    Recipe r = recipeMap.get(key);
                    if (r.contains(s)) {
                        recipeList.add(r);
                    }
                }
            } else {
                while (it.hasNext()) {
                    String key = (String) it.next();
                    Recipe r = recipeMap.get(key);
                    recipeList.add(r);
                }
            }
        }
    }

    public static void add(Recipe r) {
        recipeMap.put(r.getUUID(), r);
    }

    public static void put(String key, Recipe r) {
        recipeMap.put(key, r);
    }

    public static Recipe getRecipe(String key) {
        return recipeMap.get(key);
    }

    public static List<Recipe> getList() {
        return recipeList;
    }

    public static void remove(Recipe r) {
        RecipeList.recipeMap.remove(r.getUUID());
    }

    public static void update(Recipe r) {
        recipeMap.put(r.getUUID(), r);
    }
}
